package pit.com.ordermobilesystem.http;

public class Encryption {

    public Encryption() {
    }

    public static int getASCII(String strChar) {
        if (strChar.length() > 0) {
            return strChar.charAt(0);
        } else {
            return -1;
        }
    }

    public static String getCharFromASCII(int intAsciiValue) {
        if (intAsciiValue <= 255) {
            char[] asciiChar = {(char) intAsciiValue};
            return new String(asciiChar);
        } else {
            return "Not a valid ASCII value";
        }
    }

    private int convertHexaToInteger(String sHexa) {
        int iRet, N, iTem, i;
        String sTem = "";
        String s = "";
        int lStep;
        s = sHexa.toUpperCase();
        iRet = 0;
        lStep = 1;
        N = sHexa.length();
        i = N - 1;
        while (i > -1) {
            sTem = s.substring(i, i + 1);//get tag char				
            if (getASCII(sTem) <= getASCII("F") && getASCII(sTem) >= getASCII("A")) {
                iTem = getASCII(sTem) - 55;
            } else {
                iTem = Integer.parseInt(sTem);
            }
            iRet = iRet + iTem * lStep;
            lStep = lStep * 16;
            i--;
        }
        return iRet;
    }

    private int findMaxAscci(String s) {
        int iRet, N, Tem;
        iRet = 0;
        N = s.length();
        for (int i = 0; i < N; i++) {
            Tem = getASCII(s.substring(i, i + 1));
            if (iRet < Tem) {
                iRet = Tem;
            }
        }
        return iRet;
    }

    private String convertIntegerToHexa(int iValue) {
        String sRet, sTem, s;
        int iTem, iVTem, sRadix;
        iVTem = iValue;
        s = "";
        sRadix = 16;
        iTem = iVTem % sRadix;
        iVTem = iVTem / sRadix;

        if (iTem <= 9 && iTem >= 0) {
            sTem = "" + iTem;
            sTem = sTem.trim();
        } else {
            sTem = getCharFromASCII(iTem - 10 + 65);
        }
        sTem = sTem + s;
        s = sTem;
        while (iVTem > 0) {
            iTem = iVTem % sRadix;
            iVTem = iVTem / sRadix;

            if (iTem <= 9 && iTem >= 0) {
                sTem = "" + iTem;
                sTem = sTem.trim();
            } else {
                sTem = getCharFromASCII(iTem - 10 + 65);
            }
            sTem = sTem + s;
            s = sTem;
        }
        sRet = s;
        return sRet;
    }

    private String encodeString(String s) {
        String sRet;
        int iMark, i, N, iTem;
        N = s.length();
        iMark = findMaxAscci(s);
        sRet = convertIntegerToHexa(iMark);
        for (i = 0; i < N; i++) {
            iTem = getASCII(s.substring(i, i + 1));
            iTem = 255 + iTem - iMark;
            sRet = sRet + convertIntegerToHexa(iTem);
        }
        return sRet;
    }

    private String decodeString(String s) {
        String sRet, sMark, sTem;
        int iMark, i, N, iTem;
        sRet = "";
        N = s.length();
        if (N > 1) {
            sMark = s.substring(0, 2);
            iMark = convertHexaToInteger(sMark);
            sRet = "";
            i = 2;
            while (i < N) {
                sTem = s.substring(i, i + 2);
                iTem = convertHexaToInteger(sTem) + iMark - 255;
                String s1;
                s1 = getCharFromASCII(iTem);
                sRet = sRet + s1;
                i = i + 2;
            }
        }
        return sRet;
    }

    public String decode(String s) {
        String sRet;
        sRet = decodeString(s);
        sRet = decodeString(sRet);
        return sRet;
    }

    public String encode(String s) {
        String sRet;
        sRet = encodeString(s);
        sRet = encodeString(sRet);
        return sRet;
    }
}